//
//  BBTransitiontingAnimator.swift
//  BBAnimatedTransitioning
//
//  Created by YouMao on 2022/4/29.
//

import UIKit

/// 动画效果代理
public protocol BBTransitiontingAnimatorDelegate {
    // 动画执行效果
    func implement(_ transitionContext: UIViewControllerContextTransitioning, duration: TimeInterval) -> Void
}


public class BBTransitiontingAnimator: NSObject {

    public enum TransitionType {
        case present
        case dismiss
        case push
        case pop
    }
    
    /// 转场类型
    public var transitionType: TransitionType = .present
    
    /// 进入动画
    public var intoAnimation: BBTransitiontingAnimatorDelegate = BBIntoAnimation.default
    
    /// 退出动画
    public var outAnimation: BBTransitiontingAnimatorDelegate = BBOutAnimation.default
    
    // 动画时长 默认 0.25
    public var dutation: TimeInterval = 0.25
    
    /// 进入视图动画交互器 一般使用 UIPercentDrivenInteractiveTransition
    public var intoInteraction:UIViewControllerInteractiveTransitioning?
    /// 退出视图动画交互器
    public var outInteraction:UIViewControllerInteractiveTransitioning?
}

// MARK: - Methods
extension BBTransitiontingAnimator {
    
    // 退出动画
    func outAnimation(_ transitionContext: UIViewControllerContextTransitioning) {
        self.outAnimation.implement(transitionContext, duration: transitionDuration(using: transitionContext))
    }
    
    // 进入动画
    func intoAnimation(_ transitionContext: UIViewControllerContextTransitioning) {
        self.intoAnimation.implement(transitionContext, duration: transitionDuration(using: transitionContext))
    }
    
}
 
// MARK: UINavigationControllerDelegate
extension BBTransitiontingAnimator: UINavigationControllerDelegate {
    
    // 导航栏跳转页面执行动画
    public func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if operation == .push {
            transitionType = .push
        } else {
            transitionType = .pop
        }
        return self
    }
    
    // 导航栏跳转页面执行动画控制器
    public func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        if transitionType == .push {
            return intoInteraction
        } else {
            return outInteraction
        }
    }
}

// MARK: - UIViewControllerTransitioningDelegate
extension BBTransitiontingAnimator: UIViewControllerAnimatedTransitioning {
    
    /// 返回动画过渡的时间
    public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return dutation
    }
    
    /// 所有的过渡动画事务都在这个方法里面完成
    public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        switch transitionType {
        case .present:
            intoAnimation(transitionContext)
        case .dismiss:
            outAnimation(transitionContext)
        case .push:
            intoAnimation(transitionContext)
        case .pop:
            outAnimation(transitionContext)
        }
    }
}

// MARK: - UIViewControllerTransitioningDelegate
extension BBTransitiontingAnimator: UIViewControllerTransitioningDelegate {
    
    /// present 动画交互器
    public func interactionControllerForPresentation(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        return intoInteraction
    }
    
    /// dismiss 动画交互器
    public func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        return outInteraction
    }
    
    /// presented 动画
    public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        self.transitionType = .present
        return self
    }
    
    /// dismiss 动画交互器
    public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        self.transitionType = .dismiss
        return self
    }
    
}

